#!/bin/bash

# ***************************************************************************
# * 
# * @file:build_php.sh 
# * @author:ebayboy@163.com 
# * @date:2019-11-20 17:23 
# * @version 1.0  
# * @description: Shell script 
# * @Copyright (c)  all right reserved 
#* 
#**************************************************************************/ 

#PHP && tidy download
#https://github.com/php/php-src/archive/php-7.1.33.tar.gz
#https://github.com/htacg/tidy-html5/archive/5.6.0.tar.gz

INSTALL_DIR=/usr/local/php
CONFIG_DIR=/etc
TIDY_INSTALL_DIR=/usr/local/tidy

function build_php()
{
	./configure \
		--prefix=$INSTALL_DIR \
		--with-config-file-path=$CONFIG_DIR \
		--enable-fpm \
		--with-fpm-user=nginx \
		--with-fpm-group=nginx \
		--enable-inline-optimization \
		--disable-debug \
		--disable-rpath \
		--enable-shared \
		--enable-soap \
		--with-libxml-dir \
		--with-xmlrpc \
		--with-openssl \
		--with-mcrypt \
		--with-mhash \
		--with-pcre-regex \
		--with-sqlite3 \
		--with-zlib \
		--enable-bcmath \
		--with-iconv \
		--with-bz2 \
		--enable-calendar \
		--with-curl \
		--with-cdb \
		--enable-dom \
		--enable-exif \
		--enable-fileinfo \
		--enable-filter \
		--with-pcre-dir \
		--enable-ftp \
		--with-gd \
		--with-openssl-dir \
		--with-jpeg-dir \
		--with-png-dir \
		--with-zlib-dir \
		--with-freetype-dir \
		--enable-gd-native-ttf \
		--enable-gd-jis-conv \
		--with-gettext \
		--with-gmp \
		--with-mhash \
		--enable-json \
		--enable-mbstring \
		--enable-mbregex \
		--enable-mbregex-backtrack \
		--with-libmbfl \
		--with-onig \
		--enable-pdo \
		--with-mysqli=mysqlnd \
		--with-pdo-mysql=mysqlnd \
		--with-zlib-dir \
		--with-pdo-sqlite \
		--with-readline \
		--enable-session \
		--enable-shmop \
		--enable-simplexml \
		--enable-sockets \
		--enable-sysvmsg \
		--enable-sysvsem \
		--enable-sysvshm \
		--enable-wddx \
		--with-libxml-dir \
		--with-xsl \
		--enable-zip \
		--enable-mysqlnd-compression-support \
		--with-pear \
		--enable-opcache	\
		--with-tidy=$TIDY_INSTALL_DIR || exit 1

	make -j4 && make install || exit 1
}

function build_tidy()
{
	wget https://www.linuxidc.com/upload/2008_05/08053005418311.gz
	gunzip tidy-xxxx.tgz
	cd tidy
	cp /usr/local/libtool/bin/libtool ./libtool -af
	sh build/gnuauto/setup.sh || exit 1
	./configure --prefix=/usr/local/tidy || exit 1
	make -j4 && make install
}

function config_php()
{
	cp php.ini-production /etc/php.ini
	cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
	cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
	cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
	chmod +x /etc/init.d/php-fpm

	groupadd nginx
	useradd nginx -g nginx -s /sbin/nologin -M
}

#build_tidy;

build_php;

config_php;

/etc/init.d/php-fpm start

exit 0

